package com.pda.xinmazhi.ui.duntong;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinmazhi.R;
import com.pda.xinmazhi.bean.LatheMachinBean;
import com.pda.xinmazhi.bean.ScanBean;
import com.pda.xinmazhi.bean.ScanResultBean;
import com.pda.xinmazhi.bean.SelectBean;
import com.pda.xinmazhi.bean.request.RawInRequest;
import com.pda.xinmazhi.contract.DunTongInContract;
import com.pda.xinmazhi.dialog.AffirmDialog;
import com.pda.xinmazhi.net.ArticleBean;
import com.pda.xinmazhi.presenter.DunTongInPresenter;
import com.pda.xinmazhi.utils.PopUtisl;
import com.pda.xinmazhi.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.DUNTONGINACTIVITY)
public class DunTongInActivity extends BaseMvpActivity<DunTongInPresenter> implements DunTongInContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_batch)
    EditText tvBatch;
    @BindView(R.id.et_outNum)
    EditText etOutNum;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;

    @Override
    public DunTongInPresenter getPresenter() {
        return new DunTongInPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_duntong_in;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                presenter.scanTheQRCodeToVerify(scanBean.getCode());
            }
        });
    }
    @Override
    public void setScanBean(ScanResultBean scanBean) {
        if (scanBean.getType().equals("KW")){
            tvStorageBin.setText(scanBean.getCode());
        }else if (scanBean.getType().equals("DT")){
            tvContainer.setText(scanBean.getCode());
        }else {
            showMessage("请扫库位和吨桶！");
        }
    }

    @Override
    public void onSuccess() {
        latheBean=null;
        tvContainer.setText("");
        tvGrade.setText("");
        tvStorageBin.setText("");
        tvArticleId.setText("");
        tvBatch.setText("");
        etOutNum.setText("");
    }

    @Override
    public void getData() {

    }


    @OnClick({R.id.iv_close, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
            finishActivity();
                break;
            case R.id.tv_move_out:
                if (TextUtils.isEmpty(tvArticleId.getText())){
                    showMessage("请先选择原料！");
                    return;
                }
                if (TextUtils.isEmpty(tvStorageBin.getText())){
                    showMessage("请先选择库位！");
                    return;
                }
                if (TextUtils.isEmpty(tvContainer.getText())){
                    showMessage("请先选择吨桶！");
                    return;
                }
                if (TextUtils.isEmpty(tvBatch.getText())){
                    showMessage("批次不能为空！");
                    return;
                }
                AffirmDialog dialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        RawInRequest request=new RawInRequest();
                        request.setBatch(latheBean.getOther());
                        request.setSku(latheBean.getSku());
                        request.setStorageBinId(tvStorageBin.getText().toString());
                        request.setContainer(tvContainer.getText().toString());
                        request.setInWeight(etOutNum.getText().toString());
                        presenter.callRawIn(request);
                    }
                });
                dialog.show("确定入库吗？");

                break;
        }
    }



    @OnClick(R.id.tv_articleId)
    public void onViewClicked() {
        presenter.getArticleList();
    }
    SelectBean latheBean;
    @Override
    public void setList(List<ArticleBean> list) {
        List<SelectBean> selectBeans = new ArrayList<>();
        for (ArticleBean bean : list) {
            selectBeans.add(new SelectBean(bean.getArticleName(), bean.getArticleId(),bean.getBatch(),bean.getGrade(),bean.getSku()));
        }
        PopUtisl.showSelectPop(this, selectBeans, tvArticleId, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                latheBean = bean;
                tvArticleId.setText(bean.getName());
                tvBatch.setText(latheBean.getOther());
                tvGrade.setText(latheBean.getNum());
            }
        }, "请选择原料");
    }
}
